//
//  STNetRequset.m
//  YiYueStore
//
//  Created by zhouzhouzhou on 2018/10/16.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STNetRequset.h"
#import "BasicRequest.h"
#import "SBJson.h"


@implementation STNetRequset


+(void)netGetRequestWithUrlStr:(NSString *)urlStr withParameters:(NSDictionary *)parameters completion:(STResultObjectBlock)completion{
    BasicRequest * basicRequest = [[BasicRequest alloc]initWithParams:parameters];
    basicRequest.URL = urlStr;
    [basicRequest startGetRequestWithSuccess:^(NSURLSessionTask *task, id responseObject) {
        completion(responseObject,nil);
    } Failure:^(NSURLSessionTask *task, NSError *error) {
        if ([error isKindOfClass:[NSDictionary class]]) {
            NSDictionary * errorDic =[NSDictionary dictionaryWithDictionary:error];
            NSError * rightError = [NSError errorWithDomain:errorDic[@"message"] code:[errorDic[@"code"] integerValue] userInfo:errorDic];
            completion(nil,rightError);
        }else{
            completion(nil,error);
        }
        
    }];
   
}


+(void)netPostRequestWithUrlStr:(NSString *)urlStr withParameters:(NSDictionary *)parameters completion:(STResultObjectBlock)completion{
    BasicRequest * basicRequest = [[BasicRequest alloc]initWithParams:parameters];
    basicRequest.URL = urlStr;
    basicRequest.requestMethod = HttpRequestMethodRawPOST;
    [basicRequest startRequestWithSuccess:^(NSURLSessionTask *task, id responseObject) {
        completion(responseObject,nil);
    } Failure:^(NSURLSessionTask *task, NSError *error) {
        completion(nil,error);
    }];
}

+(void)netPostJsonRequestWithUrlStr:(NSString *)urlStr withJsonDic:(id)jsonDic completion:(STResultObjectBlock)completion{
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
//    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:urlStr parameters:nil error:nil];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
//    // 设置body
    NSString *paramsJson;
    if (jsonDic) {
        paramsJson = [[[SBJsonWriter alloc] init] stringWithObject:jsonDic];
    }else{
        paramsJson = [[[SBJsonWriter alloc] init] stringWithObject:@{}];
    }
    [request setHTTPBody: [paramsJson dataUsingEncoding:NSUTF8StringEncoding]];
    
    AFHTTPResponseSerializer *responseSerializer = [AFHTTPResponseSerializer serializer];
    responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
                                                 @"text/html",
                                                 @"text/json",
                                                 @"text/javascript",
                                                 @"text/plain",
                                                 nil];
    manager.responseSerializer = responseSerializer;
    
    [[manager dataTaskWithRequest:request uploadProgress:^(NSProgress * _Nonnull uploadProgress) {
    } downloadProgress:^(NSProgress * _Nonnull downloadProgress) {
    } completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        if ([responseObject isKindOfClass:[NSData class]]) {
           NSDictionary * dic = [[[SBJsonParser alloc] init] objectWithData:responseObject];
            completion(dic,error);
        }else{
            completion(responseObject,error);
        }
    }] resume];
}

@end
